package com.javaranger.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import org.apache.log4j.Logger;

/**
 * 安全加密
 */
public class Digests {

	private static final Logger log = Logger.getLogger(Digests.class);

	private static SecureRandom random = new SecureRandom();

	/**
	 * 生成sessionId
	 * @return
	 */
	public static synchronized String generateSessionId() {
		// Generate a byte array containing a session identifier
		byte bytes[] = new byte[16];
		random.nextBytes(bytes); //产生16字节的byte
		bytes = getDigest().digest(bytes); // 取摘要,默认是"MD5"算法
		// Render the result as a String of hexadecimal digits
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < bytes.length; i++) { //转化为16进制字符串
			byte b1 = (byte) ((bytes[i] & 0xf0) >> 4);
			byte b2 = (byte) (bytes[i] & 0x0f);
			if (b1 < 10)
				result.append((char) ('0' + b1));
			else
				result.append((char) ('A' + (b1 - 10)));
			if (b2 < 10)
				result.append((char) ('0' + b2));
			else
				result.append((char) ('A' + (b2 - 10)));
		}
		return (result.toString());
	}

	private static MessageDigest getDigest() {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			return md;
		} catch (NoSuchAlgorithmException e) {
			log.error(e);
		}
		return null;
	}
}
